package com.cmcc.es.app.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

import cc.movein.common.utils.XmlTools;

import com.cmcc.es.app.mapper.ProjectDetailSyncMapper;
import com.cmcc.es.app.model.ProjectDetailInfo;
import com.cmcc.es.app.model.ProjectDetailSyncData;
import com.cmcc.es.app.service.DataSyncService;

@Service("projectDetailSyncService")
public class ProjectDetailSyncServiceImpl implements DataSyncService {

	private Log log = LogFactory.getLog(ProjectDetailSyncServiceImpl.class);
	private static final String DATA_FORMAT = "yyyy-MM-dd HH:mm:ss";

	@Autowired
	private ProjectDetailSyncMapper projectDetailSyncMapper;

	@Transactional
	@Override
	public void doSync(String xml) {
		String decodedXml = null;
		try {
			decodedXml = new String(Base64Utils.decodeFromString(xml), "utf-8");
			log.debug(decodedXml);
			ProjectDetailSyncData data = XmlTools.xmlStringToBean(decodedXml,
					ProjectDetailSyncData.class, DATA_FORMAT);
			log.info(data);
			List<ProjectDetailInfo> insertList = data.getInsertList();
			List<ProjectDetailInfo> updateList = data.getUpdateList();
			if (updateList != null && updateList.size() > 0) {
				/*for (ProjectDetailInfo projectDetailInfo : updateList) {
					projectDetailSyncMapper.updateItem(projectDetailInfo);
				}*/
				projectDetailSyncMapper.updateList(updateList);
			}
			if (insertList != null && insertList.size() > 0) {
				projectDetailSyncMapper.insertList(insertList);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
